namespace ASPNET.StarterKit.IssueTracker.BusinessLogicLayer
{
    using ASPNET.StarterKit.IssueTracker.DataAccessLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.DirectoryServices;

    public class ITUser
    {
        private const string _activeDirectoryPath = "GC://";
        private string _DisplayName;
        private string _Email;
        private const string _filter = "(&(ObjectClass=Person)(SAMAccountName={0}))";
        private int _Id;
        private string _Password;
        private static string _path = string.Empty;
        private string _RoleName;
        private string _Username;
        private const string _windowsSAMPath = "WinNT://";

        public ITUser()
        {
            this._Username = string.Empty;
            this._Email = string.Empty;
            this._DisplayName = string.Empty;
            this._Password = string.Empty;
        }

        public ITUser(string username)
        {
            this._Username = string.Empty;
            this._Email = string.Empty;
            this._DisplayName = string.Empty;
            this._Password = string.Empty;
            this._Username = username;
        }

        public ITUser(int userId, string username, string roleName)
        {
            this._Username = string.Empty;
            this._Email = string.Empty;
            this._DisplayName = string.Empty;
            this._Password = string.Empty;
            this._Id = userId;
            this._Username = username;
            this._RoleName = roleName;
        }

        public ITUser(int userId, string username, string roleName, string email, string displayName, string password)
        {
            this._Username = string.Empty;
            this._Email = string.Empty;
            this._DisplayName = string.Empty;
            this._Password = string.Empty;
            this._Id = userId;
            this._Username = username;
            this._RoleName = roleName;
            this._Email = email;
            this._DisplayName = displayName;
            this._Password = password;
        }

        public static bool Authenticate(string username, string password)
        {
            if ((username == null) || (username.Length == 0))
            {
                throw new ArgumentOutOfRangeException("username");
            }
            if ((password == null) || (password.Length == 0))
            {
                throw new ArgumentOutOfRangeException("password");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().Authenticate(username, password);
        }

        public static ITUserCollection GetAllUsers()
        {
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetAllUsers();
        }

        public static ITUserCollection GetAllUsersByRoleName(string roleName)
        {
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetAllUsersByRoleName(roleName);
        }

        public static string GetDisplayName(string identification)
        {
            string GetDisplayName;
            if (StringType.StrCmp(Globals.UserAccountSource, "WindowsSAM", false) == 0)
            {
                string[] samPath = identification.Split(new char[] { '\\' });
                _path = "WinNT://" + samPath[0];
                try
                {
                    DirectoryEntry entryRoot = new DirectoryEntry(_path);
                    return entryRoot.Children.Find(samPath[1], "user").Properties["FullName"].Value.ToString();
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    GetDisplayName = identification;
                    ProjectData.ClearProjectError();
                    return GetDisplayName;
                    ProjectData.ClearProjectError();
                }
                return GetDisplayName;
            }
            if (StringType.StrCmp(Globals.UserAccountSource, "ActiveDirectory", false) == 0)
            {
                _path = "GC://";
                identification = identification.Substring(identification.LastIndexOf(@"\") + 1, (identification.Length - identification.LastIndexOf(@"\")) - 1);
                string userNameFilter = string.Format("(&(ObjectClass=Person)(SAMAccountName={0}))", identification);
                DirectorySearcher searcher = new DirectorySearcher(_path);
                if (searcher == null)
                {
                    return identification;
                }
                searcher.PropertiesToLoad.Add("givenName");
                searcher.PropertiesToLoad.Add("sn");
                searcher.Filter = userNameFilter;
                try
                {
                    SearchResult search = searcher.FindOne();
                    if (search != null)
                    {
                        string firstName = SearchResultProperty(search, "givenName");
                        string lastName = SearchResultProperty(search, "sn");
                        return (firstName + " " + lastName);
                    }
                    return identification;
                }
                catch (Exception exception2)
                {
                    ProjectData.SetProjectError(exception2);
                    GetDisplayName = identification;
                    ProjectData.ClearProjectError();
                    return GetDisplayName;
                    ProjectData.ClearProjectError();
                }
                return GetDisplayName;
            }
            return identification;
        }

        public static ITUser GetUserById(int userId)
        {
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetUserById(userId);
        }

        public static ITUser GetUserByUsername(string username)
        {
            if ((username == null) || (username.Length == 0))
            {
                throw new ArgumentOutOfRangeException("username");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetUserByUsername(username);
        }

        public static ITUserCollection GetUsersByProjectId(int projectId)
        {
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetUsersByProjectId(projectId);
        }

        public bool Save()
        {
            if (StringType.StrCmp(this.DisplayName.Trim(), string.Empty, false) == 0)
            {
                this.DisplayName = GetDisplayName(this.Username);
            }
            DataAccessLayerBaseClass DBLayer = DataAccessLayerBaseClassHelper.GetDataAccessLayer();
            if (this.Id <= DefaultValues.GetUserIdMinValue())
            {
                int TempId = DBLayer.CreateNewUser(this);
                if (TempId > 0)
                {
                    this._Id = TempId;
                    return true;
                }
                return false;
            }
            return DBLayer.UpdateUser(this);
        }

        private static string SearchResultProperty(SearchResult sr, string field)
        {
            if (sr.Properties[field] != null)
            {
                return StringType.FromObject(sr.Properties[field][0]);
            }
            return null;
        }

        public string DisplayName
        {
            get
            {
                return this._DisplayName;
            }
            set
            {
                this._DisplayName = value;
            }
        }

        public string Email
        {
            get
            {
                return this._Email;
            }
            set
            {
                this._Email = value;
            }
        }

        public int Id
        {
            get
            {
                return this._Id;
            }
        }

        public string Password
        {
            get
            {
                return this._Password;
            }
            set
            {
                this._Password = value;
            }
        }

        public string RoleName
        {
            get
            {
                return this._RoleName;
            }
            set
            {
                this._RoleName = value;
            }
        }

        public string Username
        {
            get
            {
                return this._Username;
            }
            set
            {
                this._Username = value;
            }
        }
    }
}

